using UnityEngine;
using System.Collections;

public class SmeltOre : SmeltingJob {
	
	Ore ore;
	bool hauled;
	bool haulingStarted;
	
	HaulingJob h;
	
	public SmeltOre(int nx, int ny, int nz, Ore nore):base(nx, ny, nz){
		ore = nore;
		jobType = 8;
		hauled = false;
		haulingStarted = false;
		h = new HaulingJob(ore, x, y, z, this);
		
		Material material = ore.material;
			if ((material.GetType()).ToString() == "Aluminum"){
				ItemList.aluminumOre.Remove(ore);
				jobLabel = "Smelt Aluminum Ore";
			} else if ((material.GetType()).ToString() == "Copper"){
				ItemList.copperOre.Remove(ore);
				jobLabel = "Smelt Copper Ore";
			} else if ((material.GetType()).ToString() == "Iron"){
				ItemList.ironOre.Remove(ore);
				jobLabel = "Smelt Iron Ore";
			} else if ((material.GetType()).ToString() == "Tin"){
				ItemList.tinOre.Remove(ore);
				jobLabel = "Smelt Tin Ore";
			} else if ((material.GetType()).ToString() == "Zinc"){
				ItemList.zincOre.Remove(ore);
				jobLabel = "Smelt Zinc Ore";
			} else {
				Debug.LogError (material.GetType());
			}
	}
	
	public bool workOn(double workspeed, Robot robot){
		bool isDone = false;
		try {
			hauled = h.j.complete;
		} catch {
			hauled = false;
		}
		if (hauled){
			isDone =  base.workOn(workspeed, robot);
		} else {
			if (!haulingStarted){
				robot.assignJob(h, true);
				haulingStarted = true;
			}
		}
		if (isDone){
			
			GameObject.Destroy(ore.item);
			GameObject obj = (GameObject)GameObject.Instantiate(UnityEngine.Resources.Load("BarPrefab"));
			obj.transform.position = new Vector3(0f, 0f, 20f);
			
			Bar b = (Bar)obj.GetComponent("Bar");
			b.item = obj;
			b.starterate(ore.material);
			
		}
		return isDone;
	}
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
